<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LocTheoDonHang
 *
 * @author Nghiatc
 */
class default_LoctheodonhangController extends ZF_DefaultController{
    //put your code here
    public function indexAction() {
        $this->view->header = "Lọc theo đơn hàng";
        $numThang = date("m");
        $numNam = date("Y");
        $numDH = 100;
        
        $model_daily = new Default_Model_Daily();
        $model_trackdonhang = new Default_Model_Trackdonhang();
        $model_donhang = new Default_Model_Donhang();
        
        $tableDataDSDH = array();
        $arrKey = array();
        
        if ($this->getRequest()->isPost()) {
            $numThang = $this->getRequest()->getParam('numThang');
            $numNam = $this->getRequest()->getParam('numNam');
            $numDH = $this->getRequest()->getParam('numDH');
            
            $numThang = $numThang > 0 && $numThang <= 12 ? $numThang : date("m");
            if($numThang > 0 && $numThang <=9){
                $numThang = "0" . $numThang;
            }
            $numNam = $numNam > 2012 ? $numNam : date("Y");
            $numDH = $numDH > 0 ? $numDH : 10;
            
            $allDaiLy = $model_daily->listAll();
            //$this->view->allDaiLy = $allDaiLy;
            if($allDaiLy){
                foreach ($allDaiLy as $valueDL) {
                    $idDL = $valueDL['MaDaiLy'];
                    $nameDL = $valueDL['TenDaiLy'];
                    $keyT = $idDL . "_" . $numThang . "_" . $numNam;
                    $arrKey[] = $keyT;
                    $objTDH = $model_trackdonhang->getById($keyT);
                    if($objTDH){
                        $DSDHTB = $objTDH['DSMaDHTuBan'];
                        $arrDHTB = explode(",", $DSDHTB);
                        if($arrDHTB){
                            $SLDHTemp = 0;
                            foreach ($arrDHTB as $idDH) {
                                $objDHTB = $model_donhang->getById($idDH);
                                if($objDHTB){
                                    //get danh sách mã chi tiết đơn hàng.
                                    $dsMaCTDH = $objDHTB['DSMaCTDH'];
                                    if($dsMaCTDH){
                                        $arrMaCTDH = explode(",", $dsMaCTDH);
                                        if($arrMaCTDH){
                                            $SLDHTemp = $SLDHTemp + count($arrMaCTDH);
                                        }
                                    }
                                }
                            }
                            //add new row into $tableDataDSDH if $SLDHTemp >= $numDH
                            if($SLDHTemp >= $numDH){
                                $row = array(
                                    'maDL' => $idDL,
                                    'tenDL' => $nameDL,
                                    'slDH' => $SLDHTemp
                                );
                                $tableDataDSDH[] = $row;
                            }
                        }
                    }
                }
            }
            
        } else { // the first view.
            $allDaiLy = $model_daily->listAll();
            //$this->view->allDaiLy = $allDaiLy;
            if($allDaiLy){
                foreach ($allDaiLy as $valueDL) {
                    $idDL = $valueDL['MaDaiLy'];
                    $nameDL = $valueDL['TenDaiLy'];
                    $keyT = $idDL . "_" . $numThang . "_" . $numNam;
                    $arrKey[] = $keyT;
                    $objTDH = $model_trackdonhang->getById($keyT);
                    if($objTDH){
                        $DSDHTB = $objTDH['DSMaDHTuBan'];
                        $arrDHTB = explode(",", $DSDHTB);
                        if($arrDHTB){
                            $SLDHTemp = 0;
                            foreach ($arrDHTB as $idDH) {
                                $objDHTB = $model_donhang->getById($idDH);
                                if($objDHTB){
                                    //get danh sách mã chi tiết đơn hàng.
                                    $dsMaCTDH = $objDHTB['DSMaCTDH'];
                                    if($dsMaCTDH){
                                        $arrMaCTDH = explode(",", $dsMaCTDH);
                                        if($arrMaCTDH){
                                            $SLDHTemp = $SLDHTemp + count($arrMaCTDH);
                                        }
                                    }
                                }
                            }
                            //add new row into $tableDataDSDH if $SLDHTemp >= $numDH
                            if($SLDHTemp >= $numDH){
                                $row = array(
                                    'maDL' => $idDL,
                                    'tenDL' => $nameDL,
                                    'slDH' => $SLDHTemp
                                );
                                $tableDataDSDH[] = $row;
                            }
                        }
                    }
                }
            }
        }
        $this->view->numThang = $numThang;
        $this->view->numNam = $numNam;
        $this->view->numDH = $numDH;
        $this->view->tableDataDSDH = $tableDataDSDH;
        
        $this->view->arrKey = $arrKey;
    }
    
    
    
}

?>
